package com.beiding.controller;

import com.beiding.exception.BadRequestException;
import com.beiding.pojo.room.Room;
import com.beiding.service.search.RoomSearchService;
import com.beiding.service.storage.RoomStorageService;
import feign.FeignException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.List;

@RestController
@RequestMapping("/myRooms")
public class ResourceController {

    private RoomSearchService roomSearchService;

    @Autowired
    public void setRoomSearchService(RoomSearchService roomSearchService) {
        this.roomSearchService = roomSearchService;
    }


    private RoomStorageService storageService;

    @Autowired
    public void setStorageService(RoomStorageService storageService) {
        this.storageService = storageService;
    }

    @PostMapping
    @Secured("ROLE_LANDLORD")
    public List<Room> myRooms(Principal principal, int page, int pageSize) {
        return roomSearchService.myRooms(principal, page, pageSize);
    }


    @PostMapping("/remove")
    @Secured("ROLE_LANDLORD")
    void remove(String id) {
            storageService.remove(id);
    }

    //获取简单信息,从本地数据库中查找
    @PostMapping("/get")
    @Secured("ROLE_LANDLORD")
    Room get(@RequestParam String id) {
        return roomSearchService.findById(id);
    }

    @PostMapping("/search")
    @Secured("ROLE_LANDLORD")
    List<Room> search(Principal principal, @RequestParam String q, int page, int pageSize) {
        return roomSearchService.search(principal, q, page, pageSize);
    }

}

